const http = require('../../../../utils/http.js');
Page({
  data: {
    collegeList: [],
    majorList: {},
    currentCollegeId: null // 新增属性，用于跟踪当前点击的学院ID
  },

  onLoad() {
    this.getCollegeList();
  },

  getCollegeList() {
    http.get('/chengkao/examUniversity/all').then(res =>{
      console.log(res);
      const collegeList = res.data.data;
        this.setData({
          collegeList: collegeList
        });
    }).catch(err=>{
      console.log(err);
      wx.showToast({
        title: '系统错误，请稍后重试',
        icon: Error
      })
    })
  },

  getMajorsByCollgeId(collegeId){
    const postData =  { universityId: collegeId }
    http.get('/chengkao/CKMajor/universityId' , postData).then(res =>{
        // console.log(res);
        const majorList = this.data.majorList;
          majorList[collegeId] = res.data.data;
          this.setData({
            majorList: majorList
          });
      }).catch(err=>{
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: Error
        })
      })
  },

  // 新增方法，用于处理学院项的点击事件
  toggleCollege(e) {
    const collegeId = e.currentTarget.dataset.id;
    // console.log("collegeId:"+collegeId)
    // 设置目前的点击院校的id
    this.setData({
      currentCollegeId: this.data.currentCollegeId === collegeId ? null : collegeId
    });
    // 根据院校id获取专业列表
    if(!this.data.majorList[collegeId]){
        this.getMajorsByCollgeId(collegeId);
    }
  }
});